package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Commodity;
import com.dingyh.shopwebbackground.entity.Orders;
import com.dingyh.shopwebbackground.entity.Stock;
import com.dingyh.shopwebbackground.entity.StockOut;
import com.dingyh.shopwebbackground.service.CommodityService;
import com.dingyh.shopwebbackground.service.OrdersService;
import com.dingyh.shopwebbackground.service.StockOutService;
import com.dingyh.shopwebbackground.service.StockService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-21
 */
@RestController
@CrossOrigin
@RequestMapping("/stockOut")
public class StockOutController {

    @Autowired
    StockOutService stockOutService;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private StockService stockService;

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/getAllStockOut")
    public CommonResult<Object> getAllStockOut(String saleId,@RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.offsetPage((pageIndex - 1) * pageSize,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("saleId",saleId);
        List<StockOut> stockOuts = stockOutService.getAllStockOut(map);
        stockOuts.forEach(stockOut -> {
            Commodity commodity = commodityService.getById(stockOut.getGoodsid());
            stockOut.setGoodsname(commodity.getTitle() );
        });
        PageInfo<StockOut> pageInfo = new PageInfo<>(stockOuts);
        return new CommonResult(200,"",pageInfo);
    }

    /**
     * 出库
     * @param goodsid
     * @param orderid
     * @param flot
     * @param qty
     * @param logisticsCode
     * @param logisticsCompany
     * @return
     */
    @RequestMapping("addStockOut")
    public  CommonResult<Object> addStockOut(String goodsid,String orderid,String flot,Long qty,String logisticsCode,String logisticsCompany){
        Stock stock = stockService.getQty(flot);
        if( stock.getQty() < qty ){
            return new CommonResult<>(500,"该批次剩余库存不足本次出库数量");
        }
        stock.setQty(stock.getQty() - qty);
        stockService.updateById(stock);
        StockOut stockOut = StockOut.builder()
                .id(UUIDUtil.generator())
                .goodsid(goodsid)
                .orderid(orderid)
                .logisticsCompany(logisticsCompany)
                .logisticsCode(logisticsCode)
                .qty(qty)
                .flot(flot)
                .timeadd(System.currentTimeMillis())
                .build();
        boolean result = stockOutService.save(stockOut);
        Map<String,Object> map = new HashMap<>();
        map.put("id",orderid);
        Orders orders = ordersService.selectOrder(map);
        orders.setState("2");
        boolean resultOrder = ordersService.updState(orders);

        if (resultOrder){
            if (result) {
                return new CommonResult(200, "提交成功！", null);
            } else {
                return new CommonResult(500, "提交失败！", null);
            }
        }else {
            return new CommonResult(500, "提交失败！", null);
        }
    }

}

